package com.jbit.dm212.service.impl;

import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.jbit.dm212.entity.po.DmLinkUser;
import com.jbit.dm212.mapper.DmLinkUserMapper;
import com.jbit.dm212.service.DmLinkUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
@Service
public class DmLinkUserServiceImpl implements DmLinkUserService {
    @Resource
    private DmLinkUserMapper linkUserMapper;
    @Override
    public List<DmLinkUser> getLinkUserByUserId(Integer userId) {
        MPJLambdaWrapper<DmLinkUser> lambdaWrapper = new MPJLambdaWrapper<>();
        lambdaWrapper.eq(DmLinkUser::getUserId,userId);
        lambdaWrapper.eq(DmLinkUser::getDeleted,0);
        return linkUserMapper.selectList(lambdaWrapper);
    }

    @Override
    public boolean addLinkUser(DmLinkUser linkUser) {
        linkUser.setCreatedTime(new Date());
        int count = linkUserMapper.insert(linkUser);
        return count == 1;
    }

    @Override
    public boolean checkIdCard(DmLinkUser linkUser) {
        MPJLambdaWrapper<DmLinkUser> lambdaWrapper = new MPJLambdaWrapper();
        lambdaWrapper.eq(DmLinkUser::getIdCard,linkUser.getIdCard());
        lambdaWrapper.eq(DmLinkUser::getUserId,linkUser.getUserId());
        lambdaWrapper.eq(DmLinkUser::getDeleted,0);

        return linkUserMapper.selectCount(lambdaWrapper) > 0;
    }

    @Override
    public List<DmLinkUser> getLinkUsers(String[] linkIds) {
        MPJLambdaWrapper<DmLinkUser> lambdaWrapper = new MPJLambdaWrapper<>();
        lambdaWrapper.select(DmLinkUser::getId,DmLinkUser::getName);
        lambdaWrapper.in(DmLinkUser::getId,linkIds);
        return linkUserMapper.selectList(lambdaWrapper);
    }

    @Override
    public boolean deleteLinkUserById(Integer id) {
        DmLinkUser linkUser = new DmLinkUser();
        linkUser.setId(id);
        linkUser.setDeleted(1);
        return linkUserMapper.updateById(linkUser) == 1;
    }

}
